from django.shortcuts import render
from django.views.generic.base import View
from apps.courses.models import Course, CourseTag, CourseResource, Vedio
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from apps.operations.models import UserFavorite, UserCourse, CourseComments
from django.db.models import Q
#登录验证
from django.contrib.auth.mixins import LoginRequiredMixin

class CourseListView(View):
    def get(self, request, *args, **kwargs):
        """获取课程列表"""
        all_courses  = Course.objects.order_by("-add_time")
        hot_courses = Course.objects.order_by("-click_nums")[:3]

        #搜索关键词
        keywords = request.GET.get("keywords", "")
        if keywords:
            all_courses = all_courses.filter(Q(name__icontains=keywords)|Q(desc__icontains=keywords)|Q(detail__icontains=keywords))
        s_type = "course"
        #课程排排序

        sort = request.GET.get("sort", "")
        if sort == "students":
            all_courses = Course.objects.order_by("-students")
        elif sort == "hot":
            all_courses = Course.objects.order_by("-click_nums")

        # 对课程数据进行分页
        try:
            page = request.GET.get('page', 1)
        except PageNotAnInteger:
            page = 1

        p = Paginator(all_courses, 6, request=request)

        orgs = p.page(page)
        return render(request, "course-list.html", {
            "all_courses":orgs,
            "sort":sort,
            "hot_courses":hot_courses,
            "keywords":keywords,
            "s_type":s_type
        })

class CourseDetailView(View):
    def get(self, request,course_id, *args, **kwargs):
        """获取课程详情"""
        course = Course.objects.get(id=int(course_id))
        course.click_nums+=1
        course.save()
        #获取收藏状态
        has_fav_course = False
        has_fav_org = False
        if request.user.is_authenticated:
            if UserFavorite.objects.filter(user=request.user, fav_id=course_id, fav_type=1):
                has_fav_course = True

            if UserFavorite.objects.filter(user=request.user, fav_id=course.course_org.id, fav_type=2):
                has_fav_org = True
        # 通过课程的TAG做课程的推荐
        # tag = course.tag
        # related_course = []
        # 不查询当前的课程id列表id__in=[course.id]
        # if tag:
        #     related_course=Course.objects.filter(tag=tag).exclude(id=course.id)[:1]
        tags = course.coursetag_set.all()
        #列表生成式
        tag_list = [tag.tag for tag in tags]
        course_tags = CourseTag.objects.filter(tag__in=tag_list).exclude(course_id=course.id)
        related_courses = set()
        for course_tag in course_tags:
            related_courses.add(course_tag.course)
        # tag_list = []
        # for tag in tags:
        #     tag_list.append(tag.tag)
        return render(request, "course-detail.html", {
            "course":course,
            "has_fav_org": has_fav_org,
            "has_fav_course": has_fav_course,
            "related_courses":related_courses
        })

class CourseLessonView(LoginRequiredMixin, View):
    # 对view进行login登录验证
    login_url = "/login/"
    def get(self, request, course_id, *args, **kwargs):
        """
        获取课程章节信息
        """
        course = Course.objects.get(id=int(course_id))
        course.click_nums += 1
        course.save()
        #1. 查询用户是否关联该课程
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students+=1
            course.save()
        #学习过该课程的所有同学
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(user_id__in=user_ids).order_by("-course__click_nums")[:5]
        # related_courses = [user_course.course  for user_course in all_courses if user_course.course.id!=course.id]
        related_courses = []
        for item in all_courses:
            if item.course_id!=course.id:
                related_courses.append(item.course)
        course_resource = CourseResource.objects.filter(course=course)

        #3. 其他课程
        return render(request, "course-video.html", {
            "course": course,
            "course_resource": course_resource,
            "related_courses":related_courses

        })


class CourseCommentView(LoginRequiredMixin, View):
    # 对view进行login登录验证
    login_url = "/login/"
    def get(self, request, course_id, *args, **kwargs):
        """
        获取课程章节信息
        """
        course = Course.objects.get(id=int(course_id))
        course.click_nums += 1
        course.save()
        comments = CourseComments.objects.filter(course=course)
        #1. 查询用户是否关联该课程
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students+=1
            course.save()
        #学习过该课程的所有同学
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(user_id__in=user_ids).order_by("-course__click_nums")[:5]
        # related_courses = [user_course.course  for user_course in all_courses if user_course.course.id!=course.id]
        related_courses = []
        for item in all_courses:
            if item.course_id!=course.id:
                related_courses.append(item.course)
        course_resource = CourseResource.objects.filter(course=course)

        #3. 其他课程
        print(comments)
        return render(request, "course-comment.html", {
            "course": course,
            "course_resource": course_resource,
            "related_courses":related_courses,
            "comments":comments

        })


class VideoView(LoginRequiredMixin, View):
    login_url = "/login/"
    def get(self, request, course_id,video_id, *args, **kwargs):
        """
        获取课程章节信息
        """
        course = Course.objects.get(id=int(course_id))
        course.click_nums += 1
        course.save()

        video = Vedio.objects.get(id=int(video_id))
        #1. 查询用户是否关联该课程
        user_courses = UserCourse.objects.filter(user=request.user, course=course)
        if not user_courses:
            user_course = UserCourse(user=request.user, course=course)
            user_course.save()
            course.students+=1
            course.save()
        #学习过该课程的所有同学
        user_courses = UserCourse.objects.filter(course=course)
        user_ids = [user_course.user.id for user_course in user_courses]
        all_courses = UserCourse.objects.filter(user_id__in=user_ids).order_by("-course__click_nums")[:5]
        # related_courses = [user_course.course  for user_course in all_courses if user_course.course.id!=course.id]
        related_courses = []
        for item in all_courses:
            if item.course_id!=course.id:
                related_courses.append(item.course)
        course_resource = CourseResource.objects.filter(course=course)

        #3. 其他课程

        return render(request, "course-play.html", {
            "course": course,
            "course_resource": course_resource,
            "related_courses": related_courses,
            "video": video,
        })